//
//  UserListTableViewCell.m
//  HJZWeiBo
//
//  Created by 蒋晨成 on 16/5/30.
//  Copyright © 2016年 蒋晨成. All rights reserved.
//

#import "UserListTableViewCell.h"
#import <UIImageView+WebCache.h>
#import "NSString+Font.h"
@implementation UserListTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.iconImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 60, 60)];
        self.iconImageView.layer.cornerRadius = 30;
        self.iconImageView.layer.masksToBounds = YES;
        [self.contentView addSubview:_iconImageView];
        
        
        self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(75, 10, 0, 30)];
        [self.contentView addSubview:_nameLabel];
        
        self.statusLabel = [[UILabel alloc] initWithFrame:CGRectMake(75, 40, kWidth - 150, 20)];
        [self.contentView addSubview:_statusLabel];
        self.statusLabel.textColor = [UIColor grayColor];
        self.statusLabel.font = [UIFont systemFontOfSize:13];
        
        self.followingButton = [UIButton buttonWithType:UIButtonTypeCustom];
        self.followingButton.frame = CGRectMake(kWidth - 90, 25, 80, 30);
        [self.followingButton setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
        [self.contentView addSubview:_followingButton];
        [self.followingButton addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return self;
}


- (void)setUser:(WeiboUser *)user {
    _user = user;
    [_iconImageView sd_setImageWithURL:[NSURL URLWithString:user.avatarHDUrl]];
   
    CGFloat width = [user.name getTextWidthWithFont:[UIFont systemFontOfSize:17] height:30];
    CGRect newFrame = _nameLabel.frame;
    newFrame.size.width = width;
    _nameLabel.frame = newFrame;
    _nameLabel.text = user.name;
    _statusLabel.text = user.userDescription;
    
    
    if ([_user.userID isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:@"userID"]]) {
        [_followingButton removeFromSuperview];
        return;
    }
    
    
    if ([_userType isEqualToString:@"friend"]) {
        if (user.isFollowingMe && user.isFollowingByMe) {
            [_followingButton setTitle:@"相互关注" forState:UIControlStateNormal];
        }else {
            [_followingButton setTitle:@"已关注" forState:UIControlStateNormal];
        }
    }else {
        if (user.isFollowingByMe && user.isFollowingByMe) {
            [_followingButton setTitle:@"相互关注" forState:UIControlStateNormal];
        }else {
            [_followingButton setTitle:@"加关注" forState:UIControlStateNormal];
        }
    }
        
    
    
   
    
}

- (void)buttonAction:(UIButton *)button {
    
    if ([self.delegate respondsToSelector:@selector(followUserWithCell:isSelf:)]) {
        if ([[button titleForState:UIControlStateNormal] isEqualToString:@"相互关注"]) {
            [self.delegate followUserWithCell:self isSelf:YES];
        }else if ([[button titleForState:UIControlStateNormal] isEqualToString:@"已关注"]) {
            [self.delegate followUserWithCell:self isSelf:YES];
        }else if ([[button titleForState:UIControlStateNormal] isEqualToString:@"加关注"]){
            [self.delegate followUserWithCell:self isSelf:NO];
        }
        
        
    }
}

@end
